<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\Providers\MyCurlServiceProvider;

class CarVin extends Model
{

    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'car_vin';
    public static $vin_source_url = 'http://wechat.superepc.com:8080/vtm/query?grant_code=L9L6HkGH&vin=';
    public static function getInfo( $vin ){

        $carVin = CarVin::where('vin', $vin)->first();

        if( !empty( $carVin ) ){
            return json_decode($carVin->json, true);
        }else{

            //todo 到 copton 抓取数据
            $url = self::$vin_source_url . $vin;
            $res = MyCurlServiceProvider::send( $url );
            $res_arr = json_decode( $res, true);
//            print_r( $res_arr );die();
            if( !empty($res_arr['size']) ){
                $carVin = new CarVin();
                $carVin->vin = $vin;
                $carVin->json= json_encode( $res_arr['result'], JSON_UNESCAPED_UNICODE );
                $carVin->save();
                return $res_arr['result'];
            }else{
                return null;
            }
        }

    }

}